package com.ujiuye.demo;

import com.ujiuye.bean.Member;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;
import java.util.Set;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-mongodb.xml"})
public class MongoTest {
    @Autowired
    MongoTemplate mongoTemplate;

    /**
     * 查询当前库中集合的名称。在测试前，确保mongod服务已开启
     */
    @Test
    public void test1() throws Exception {
        Set<String> s = mongoTemplate.getCollectionNames();
        for (String col : s) {
            System.out.println(col);
        }
    }

    /**
     * 插入数据
     */
    @Test
    public void test2() throws Exception {
        Member member = new Member();
        member.setM_nickname("demo");
       /* member.setM_age(56);
        member.setM_code("zyh2001");*/
//        member.setM_nickname("jack");
//        member.setM_regtime(new Date());
        // insert存在则不做处理，不存在则插入
        mongoTemplate.insert(member);
    }

    /**
     * 单条件查询
     */
    @Test
    public void test3() throws Exception {
        Query query = new Query();
        query.addCriteria(Criteria.where("m_code").is("zyh2001"));
        List<Member> list = mongoTemplate.find(query, Member.class);
        for (Member member : list) {
            System.out.println(member);
        }
    }

    /**
     * 多条件查询
     */
    @Test
    public void test4() throws Exception {
        Query query = new Query();
        query.addCriteria(
                Criteria.where("m_code").is("zyh2001").and("m_age").lte(21));
        List<Member> list = mongoTemplate.find(query, Member.class);
        for (Member member : list) {
            System.out.println(member);
        }
    }

    /**
     * 分页查询
     */
    @Test
    public void test5() throws Exception {
        // 查询所有
        // List<Member> list = mongoTemplate.findAll(Member.class);

        Query query = new Query();
        query.addCriteria(Criteria.where("m_age").lt(50));// m_age<50
        query.with(new Sort(
                new Sort.Order(Sort.Direction.DESC, "m_age")
        ));//m_age降序
        query.skip(1);
        query.limit(2);
        List<Member> list = mongoTemplate.find(query, Member.class);
        for (Member member : list) {
            System.out.println(member);
        }
    }

   /* *
     * 删除
     */
    @Test
    public void test6() throws Exception {
        Query query = new Query(Criteria.where("m_age").is(21));
        mongoTemplate.remove(query, Member.class);
    }

    /**
     * 更新数据
     */
    @Test
    public void test7() throws Exception {
        //将name=jack修改name=jack2
        Query query = new Query();
        query.addCriteria(Criteria.where("name").is("jack"));

        Update update = new Update();
        update.set("name", "jack2");

        //参数1：query对象，参数2：update对象，参数3：集合名称
        mongoTemplate.updateMulti(query, update, "member");
    }

}
